package com.cb.admin.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cb.admin.domain.MemuInfo;
import com.cb.admin.service.IMemuInfoService;
import com.cb.constants.Constants;
import com.cb.util.DateUtils;
import com.cb.util.JSONUtil;


@Controller
@RequestMapping("/menuinfo")
public class MenuInfoController extends BaseController {
	
	@Autowired
	private IMemuInfoService memuInfoService;
	
	@RequestMapping("show")
	public String showMenuInfo(Model model){
		return "/menu/menuInfoList";
	}
	
	@RequestMapping(path = "/getList", method = RequestMethod.POST)
	@ResponseBody
    public String getList(MemuInfo menu){
		//当前页  
		menu.setStart((menu.getPage()-1)*menu.getRows());
		menu.setLength(menu.getRows());
				
		List<MemuInfo> arr = memuInfoService.query(menu);
		
		JSONObject jo = new JSONObject();
		
		int size = memuInfoService.querySize(menu);
		
		jo.put("rows", arr);
		jo.put("total", size);
		
		return jo.toString();
	}
	
	@RequestMapping("createPage")
	public String createPage(Model model,@RequestParam("parentId") int parentId){
		model.addAttribute("parentId", parentId);
		return "/menu/menuInfoDetail";
	}
	
	@RequestMapping(path = "/create", method = RequestMethod.POST)
	@ResponseBody
	public String createMenu(MemuInfo menu,HttpServletRequest request,HttpSession session){
		
		JSONObject jo = new JSONObject();
		
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		menu.setDeleteFlag(Constants.deleteFlag.deleteFlag_1);
		menu.setCreateAt(DateUtils.getNowTime());
		menu.setCreateUser(returnSessionObj(session).getAccount());
		menu.setUpdateAt(DateUtils.getNowTime());
		menu.setUpdateUser(returnSessionObj(session).getAccount());
		
		try{
			memuInfoService.insert(menu);
		} catch(Exception e){
			JSONUtil.returnFailReuslt(null);
		}
		
		return jo.toString();
		
	}
		
	@RequestMapping("deleteById")
	@ResponseBody
	public String deleteById(Model model,@RequestParam("Ids") String Ids) {
		
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		for(String id :Ids.split(",")) {
			memuInfoService.deleteById(Integer.parseInt(id));
		}
		
		return jo.toString();
	}
	
	@RequestMapping("edit")
	public String editById(Model model,@RequestParam("id") int id){
		MemuInfo menu = memuInfoService.queryById(id);
		
		model.addAttribute("menuInfo", menu);
		return "/menu/menuInfoEdit";
	}
	
	@RequestMapping(path = "/update", method = RequestMethod.POST)
	@ResponseBody
	public String updateById(MemuInfo menu,HttpServletRequest request,HttpSession session){
		JSONObject jo = new JSONObject();
		
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		menu.setUpdateAt(DateUtils.getNowTime());
		menu.setUpdateUser(returnSessionObj(session).getAccount());
		
		try {
			memuInfoService.update(menu);
		} catch (Exception e) {
			JSONUtil.returnFailReuslt(null);
		}
		
		return jo.toString();
	}
}